home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 4 / QRZ Ham Radio Callsign Database - Volume 4.iso / files / tcpip / amiga / asrc29p.lha / ttylink.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-29  |  3.7 KB  |  186 lines

  1. #include <stdio.h>
  2. #include "global.h"
  3. #include "mbuf.h"
  4. #include "socket.h"
  5. #include "telnet.h"
  6. #include "session.h"
  7. #include "proc.h"
  8. #include "tty.h"
  9. #include "mailbox.h"
  10. #include "commands.h"
  11. #include "config.h"
  12. #include "files.h"
  13.  
  14. static int Sttylink = -1;    /* Protoype socket for service */
  15. #ifdef    MAILBOX
  16. static int Stelnet = -1;
  17. #endif
  18. static char Tnbanner1[] = "Welcome to TTY-Link at %s\n";
  19. static char Tnbanner2[] = "The System is Unattended";
  20. extern int Attended;
  21.  
  22. int ttylstart(argc,argv,p)
  23. int argc;
  24. char *argv[];
  25. void *p;
  26. {
  27.     struct sockaddr_in lsocket;
  28.     int s,type;
  29.  
  30.     if(Sttylink != -1){
  31.         freeargs(argc,argv);
  32.         return 0;
  33.     }
  34.     psignal(Curproc,0);    /* Don't keep the parser waiting */
  35.     chname(Curproc,"TTYlink listener");
  36.  
  37.     lsocket.sin_family = AF_INET;
  38.     lsocket.sin_addr.s_addr = INADDR_ANY;
  39.     if(argc < 2)
  40.         lsocket.sin_port = IPPORT_TTYLINK;
  41.     else
  42.         lsocket.sin_port = atoi(argv[1]);
  43.  
  44.     freeargs(argc,argv);
  45.     Sttylink = socket(AF_INET,SOCK_STREAM,0);
  46.     bind(Sttylink,(char *)&lsocket,sizeof(lsocket));
  47.     listen(Sttylink,1);
  48.     for(;;){
  49.         if((s = accept(Sttylink,NULLCHAR,(int *)NULL)) == -1)
  50.             break;    /* Service is shutting down */
  51.         
  52.         if(availmem() < Memthresh){
  53.             usprintf(s,"System is overloaded; try again later\r\n");
  54.             shutdown(s,1);
  55.         } else {
  56.             type = TELNET;
  57.             newproc("ttylink",2048,ttylhandle,s,(void *)&type,NULL);
  58.         }
  59.     }
  60.     return 0;
  61. }
  62. /* This function handles all incoming "ttylink" sessions, be they TCP,
  63.  * NET/ROM or AX.25
  64.  */
  65. void ttylhandle(s,t,p)
  66. int s;
  67. void *t;
  68. void *p;
  69. {
  70.     int type,index;
  71.     struct session *sp;
  72.     char addr[MAXSOCKSIZE],buf2[77];
  73.     int len = MAXSOCKSIZE;
  74.     struct telnet tn;
  75.     FILE *fp;
  76.  
  77.     type = * (int *)t;
  78.     sockowner(s,Curproc);    /* We own it now */
  79.     mainlog(s,"open %s",Sestypes[type]);
  80.  
  81.     if(!Attended){
  82.         usprintf(s, "%s\n",
  83.             ((p = getenv("MBATND")) == NULL) ? Tnbanner2 : p);
  84.         close_s(s);
  85.         mainlog(s,"close %s - Unattended",Sestypes[type]);
  86.         return;
  87.     }
  88.  
  89.     /* Allocate a session descriptor */
  90.     if((sp = newsession(NULLCHAR,type,1)) == NULLSESSION){
  91.         usprintf(s,"Too many sessions\n");
  92.         close_s(s);
  93.         mainlog(s,"close %s - Too many sessions",Sestypes[type]);
  94.         return;
  95.     }
  96.     index = sp - Sessions;
  97.  
  98.     /* Initialize a Telnet protocol descriptor */
  99.     memset((char *)&tn,0,sizeof(tn));
  100.     tn.session = sp;    /* Upward pointer */
  101.     sp->cb.telnet = &tn;    /* Downward pointer */
  102.     sp->s = s;
  103.     sp->proc = Curproc;
  104.  
  105.     getpeername(s,addr,&len);
  106.     tprintf("\007Incoming %s session %u from %s\007\n",
  107.         Sestypes[type],index,psocket(addr));
  108.  
  109.     usprintf(s, Tnbanner1, Hostname);
  110.  
  111.     sprintf(buf2,"%s/TTY.SignOn",SignOn);
  112.     if((fp = fopen(buf2,READ_TEXT)) != NULLFILE) {
  113.         sendfile(fp,s,ASCII_TYPE,0);
  114.         fclose(fp);
  115.     }
  116.  
  117.     tnrecv(&tn);
  118. }
  119.  
  120. /* Shut down Ttylink server */
  121. int ttyl0(argc,argv,p)
  122. int argc;
  123. char *argv[];
  124. void *p;
  125. {
  126.     close_s(Sttylink);
  127.     Sttylink = -1;
  128.     return 0;
  129. }
  130.  
  131. #ifdef    MAILBOX
  132. /* Start up Telnet server */
  133. int telnet1(argc,argv,p)
  134. int argc;
  135. char *argv[];
  136. void *p;
  137. {
  138.     struct sockaddr_in lsocket;
  139.     int s;
  140.     int type;
  141.  
  142.     if(Stelnet != -1){
  143.         freeargs(argc,argv);
  144.         return 0;
  145.     }
  146.     psignal(Curproc,0);     /* Don't keep the parser waiting */
  147.     chname(Curproc,"Telnet listener");
  148.  
  149.     lsocket.sin_family = AF_INET;
  150.     lsocket.sin_addr.s_addr = INADDR_ANY;
  151.     if(argc < 2)
  152.         lsocket.sin_port = IPPORT_TELNET;
  153.     else
  154.         lsocket.sin_port = atoi(argv[1]);
  155.     freeargs(argc,argv);
  156.     Stelnet = socket(AF_INET,SOCK_STREAM,0);
  157.     bind(Stelnet,(char *)&lsocket,sizeof(lsocket));
  158.     listen(Stelnet,1);
  159.     for(;;){
  160.         if((s = accept(Stelnet,NULLCHAR,(int *)NULL)) == -1)
  161.             break;    /* Service is shutting down */
  162.  
  163.         if(availmem() < Memthresh){
  164.             shutdown(s,1);
  165.         } else {
  166.             /* Spawn a server */
  167.             type = TELNET;
  168.             newproc("mbox",2048,mbx_incom,s,(void *)&type,NULL);
  169.         }
  170.     }
  171.     return 0;
  172. }
  173.  
  174. /* Stop telnet server */
  175. int telnet0(argc,argv,p)
  176. int argc;
  177. char *argv[];
  178. void *p;
  179. {
  180.     close_s(Stelnet);
  181.     Stelnet = -1;
  182.     return 0;
  183. }
  184. #endif
  185.  
  186.